﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
namespace BAL
{
    public class PayLog : BaseBAL<PayLogInfo, Models.PayLog>
    {
        private static string CacheName = "AllPayLog";
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.PayLogs.ToList(), 0, 60);
            }
        }

        public static PayLogInfo GetInfoByOrderId(string orderid)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.PayLog> paylogInfo = (IList<Models.PayLog>)CacheHelper.GetCache(CacheName);
            paylogInfo = paylogInfo.Where(t => t.OrderId == orderid).ToList();
            if (paylogInfo.Count == 1)
            {
                return paylogInfo.Select(t => TransT2ToT1(t)).First();
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="InstInfo">如果id=0则新增>0时编辑</param>
        /// <returns></returns>
        public static PayLogInfo Save(PayLogInfo InstInfo)
        {
            PayLogInfo paylogInfo = InstInfo;
            if (paylogInfo.Id > 0)
            {
                Edit(InstInfo);
            }
            else
            {
                paylogInfo = Add(InstInfo);
            }
            ToCache();
            return paylogInfo;
        }
    }
}
